/******************************************************************************
 *                  Shanghai ChipON Micro-Electronic Co.,Ltd
 ******************************************************************************
 *  @File Name        : kf8a100fxx_ccp.c
 *  @Author           : ChipON AE/FAE Group
 *  @Date             : 2023-12-19
 *  @HW Version       : KF8A100Fxx
 *  @Lib Version      : V2.0.1
 *  @Description      : This file provides the driver for the ccp module
 ******************************************************************************
 *  Copyright (C) by Shanghai ChipON Micro-Electronic Co.,Ltd
 *  All rights reserved.
 *
 *  This software is copyright protected and proprietary to
 *  Shanghai ChipON Micro-Electronic Co.,Ltd.
 *****************************************************************************/
/******************************************************************************
 *  |Date        |Version  |Author       |Description
 ******************************************************************************
 *  |2023-12-25  |V2.0     |Wang Junxiong|New creat
 *****************************************************************************/

/******************************************************************************
 **                     QAC Warnings
 *****************************************************************************/
/* PRQA S 0436 EOF #8-bit MCU Features: Direct Write Registers. */
/* PRQA S 1291, 1820, 1821, 1822, 1823, 1851, 1853, 3715, 3757, 3760 EOF #Registers are essentially unsigned numbers. */
/* PRQA S 2754 EOF #Level 7 problem due to unsupported assembly instructions. */
/* PRQA S 3768, 3771, 4130, 4393, 4434, 4443, 4446, 4461, 4532 EOF #Registers are essentially unsigned numbers. */

/******************************************************************************
 *                      Include Files
 *****************************************************************************/
#include "kf8a100fxx_ccp.h"
/******************************************************************************
 *                      Macro Definitions
 *****************************************************************************/

/******************************************************************************
 *                      Constants
 *****************************************************************************/

/******************************************************************************
 *                      Type definitions
 *****************************************************************************/

/******************************************************************************
 *                      Variables
 *****************************************************************************/

/******************************************************************************
 *                      Function definitions
 *****************************************************************************/

/**
 * @brief T3 Comparison mode initialization
 *
 * @param Mode Compare mode
 * @param Reload Reload Enable
 * @param Div prescale
 * @param Period T3REH/CCP3H, T3REL/CCP3L
 */
void Ccp3_CompareInit(Ccp_CompareModeType Mode, Ccp_ReloadType Reload, Ccp3_4_PrescalerType Div, uint16_t Period)
{
    uint8_t Source, RegTemp = SCF0;
    Source = SCF1 << 1u;
    Source += SCF1;
    Source ^= RegTemp;
    Source = Source << 1u;

    T3ON = 0u;
    /* Overloaded value setting */
    T3REH = (uint8_t)(Period >> 8u);
    T3REL = (uint8_t)Period;

    CCP3H = (uint8_t)(Period >> 8u);
    CCP3L = (uint8_t)Period;
    /* Enable reload, disable buzzer output, divide frequency, select clock, enable T3 */
    T3H     = 0u;
    T3L     = 0u;
    CCP3CTL = 0u;
    CCP3CTL = (uint8_t)Mode;
    if ((Mode > CCP_COMPARE_DISABLE) && (Mode < CCP_COMPARE_SOFTWARE_INTERRUPT))
    {
        CCP3OE = 1u;
    }
    else
    {
        /* nothing */
    }

    T3CTL = 0x01u | (uint8_t)Div | (uint8_t)Source | (uint8_t)Reload;
}

/**
 * @brief T4 Comparison mode initialization
 *
 * @param Mode Compare mode
 * @param Reload Reload Enable
 * @param Div prescale
 * @param Period T4REH, T4REL
 */
void Ccp4_CompareInit(Ccp_CompareModeType Mode, Ccp_ReloadType Reload, Ccp3_4_PrescalerType Div, uint16_t Period)
{
    uint8_t Source, RegTemp = SCF0;
    Source = SCF1 << 1u;
    Source += SCF1;
    Source ^= RegTemp;
    Source = Source << 1u;

    T4ON = 0u;
    /* Overloaded value setting */
    T4REH = (uint8_t)(Period >> 8u);
    T4REL = (uint8_t)Period;

    CCP4H = (uint8_t)(Period >> 8u);
    CCP4L = (uint8_t)Period;
    /* Enable reload, disable buzzer output, divide frequency, select clock, enable T4 */
    T4H     = 0u;
    T4L     = 0u;
    CCP4CTL = 0u;
    CCP4CTL = (uint8_t)Mode;
    if ((Mode > CCP_COMPARE_DISABLE) && (Mode < CCP_COMPARE_SOFTWARE_INTERRUPT))
    {
        CCP4OE = 1u;
    }
    else
    {
        /* nothing */
    }

    T4CTL = 0x01u | (uint8_t)Div | (uint8_t)Source | (uint8_t)Reload;
}

/**
 * @brief T1 Comparison mode initialization
 *
 * @param Mode Compare mode
 * @param Reload Reload Enable
 * @param Div prescale
 * @param Period PP1, PP2
 */
void Ccp5_CompareInit(Ccp_CompareModeType Mode, Ccp_ReloadType Reload, Ccp5_PrescalerType Div, uint16_t Period)
{
    /* Initialize T1 */
    T1ON    = 0u;
    T1CLKSE = 0u;
    T1CTL   = (uint8_t)Div | (uint8_t)Reload;
    T1H     = 0u;
    T1L     = 0u;
    PP2     = (uint8_t)(Period >> 8u);
    PP1     = (uint8_t)Period;
    /* Initialize CCP5 */
    PWM5L1   = (uint8_t)(Period >> 8u);
    PWM5L0   = (uint8_t)Period;
    PWM5CTL0 = 0u;
    PWM5CTL0 = (uint8_t)Mode;
    T1ON     = 1u;
}

/**
 * @brief CCP3 set period value
 *
 * @param Period period value
 */
void Ccp3_SetPeriodValue(uint16_t Period)
{
    T3REH = (uint8_t)(Period >> 8u);
    T3REL = (uint8_t)Period;

    CCP3H = (uint8_t)(Period >> 8u);
    CCP3L = (uint8_t)Period;
    T3H   = 0u;
    T3L   = 0u;
}

/**
 * @brief CCP4 set period value
 *
 * @param Period period value
 */
void Ccp4_SetPeriodValue(uint16_t Period)
{
    T4REH = (uint8_t)(Period >> 8u);
    T4REL = (uint8_t)Period;

    CCP4H = (uint8_t)(Period >> 8u);
    CCP4L = (uint8_t)Period;
    T4H   = 0u;
    T4L   = 0u;
}

/**
 * @brief CCP5 set period value
 *
 * @param Period period value
 */
void Ccp5_SetPeriodValue(uint16_t Period)
{
    PP2 = (uint8_t)(Period >> 8u);
    PP1 = (uint8_t)Period;

    PWM5L1 = (uint8_t)(Period >> 8u);
    PWM5L0 = (uint8_t)Period;
    T1H    = 0u;
    T1L    = 0u;
}

/**
 * @brief CCP3 set compare mode
 *
 * @param Mode Compare mode
 */
void Ccp3_SetCompareMode(Ccp_CompareModeType Mode)
{
    CCP3CTL &= 0xf0u;
    CCP3CTL |= (uint8_t)Mode;
}

/**
 * @brief CCP4 set compare mode
 *
 * @param Mode Compare mode
 */
void Ccp4_SetCompareMode(Ccp_CompareModeType Mode)
{
    CCP4CTL &= 0xf0u;
    CCP4CTL |= (uint8_t)Mode;
}
/**
 * @brief CCP5 set compare mode
 *
 * @param Mode Compare mode
 */
void Ccp5_SetCompareMode(Ccp_CompareModeType Mode)
{
    PWM5CTL0 &= 0xf0u;
    PWM5CTL0 |= (uint8_t)Mode;
}

/**
 * @brief T3 Capture mode initialization
 *
 * @param Mode Capture mode
 * @param Reload Reload Enable
 * @param Div prescale
 * @param Period T3REH, T3REL
 */
void Ccp3_CaptureInit(Ccp_CaptureModeType Mode, Ccp_ReloadType Reload, Ccp3_4_PrescalerType Div, uint16_t Period)
{
    uint8_t Source, RegTemp = SCF0;
    Source = SCF1 << 1u;
    Source += SCF1;
    Source ^= RegTemp;
    Source = Source << 1u;

    T3ON = 0u;
    /* Overloaded value setting */
    T3REH = (uint8_t)(Period >> 8u);
    T3REL = (uint8_t)Period;
    /* Enable reload, disable buzzer output, divide frequency, select clock, enable T3 */
    T3H     = 0u;
    T3L     = 0u;
    CCP3H   = 0u;
    CCP3L   = 0u;
    CCP3CTL = 0u;
    CCP3CTL = (uint8_t)Mode;
    T3CTL   = 0x01u | (uint8_t)Div | (uint8_t)Source | (uint8_t)Reload;
}

/**
 * @brief T4 Capture mode initialization
 *
 * @param Mode Capture mode
 * @param Reload Reload Enable
 * @param Div prescale
 * @param Period T4REH, T4REL
 */
void Ccp4_CaptureInit(Ccp_CaptureModeType Mode, Ccp_ReloadType Reload, Ccp3_4_PrescalerType Div, uint16_t Period)
{
    uint8_t Source, RegTemp = SCF0;
    Source = SCF1 << 1u;
    Source += SCF1;
    Source ^= RegTemp;
    Source = Source << 1u;

    T4ON = 0u;
    /* Overloaded value setting */
    T4REH = (uint8_t)(Period >> 8u);
    T4REL = (uint8_t)Period;
    /* Enable reload, disable buzzer output, divide frequency, select clock, enable T4 */
    T4H     = 0u;
    T4L     = 0u;
    CCP4H   = 0u;
    CCP4L   = 0u;
    CCP4CTL = 0u;
    CCP4CTL = (uint8_t)Mode;
    T4CTL   = 0x01u | (uint8_t)Div | (uint8_t)Source | (uint8_t)Reload;
}

/**
 * @brief T1 CCP5 Capture mode initialization
 *
 * @param Mode Capture mode
 * @param Reload Reload Enable
 * @param Div prescale
 * @param Period PP2 PWM5L1, PP1 PWM5L0
 */
void Ccp5_CaptureInit(Ccp_CaptureModeType Mode, Ccp_ReloadType Reload, Ccp5_PrescalerType Div, uint16_t Period)
{
    /* Initialize T1 */
    T1ON    = 0u;
    T1CTL   = (uint8_t)Div | (uint8_t)Reload;
    T1CLKSE = 0u;

    T1H = 0u;
    T1L = 0u;

    PP2 = (uint8_t)(Period >> 8u);
    PP1 = (uint8_t)Period;
    /* Initialize CCP5 */
    PWM5L1   = 0u;
    PWM5L0   = 0u;
    PWM5CTL0 = 0u;
    PWM5CTL0 = (uint8_t)Mode;
    T1ON     = 1u;
}

/**
 * @brief CCP3 gets the capture value
 *
 * @return uint16_t capture value
 */
uint16_t Ccp3_GetCaptureValue(void)
{
    uint16_t temp = 0u;

    temp = CCP3H;
    temp <<= 8u;
    temp += CCP3L;
    return temp;
}

/**
 * @brief CCP4 gets the capture value
 *
 * @return uint16_t capture value
 */
uint16_t Ccp4_GetCaptureValue(void)
{
    uint16_t temp = 0u;

    temp = CCP4H;
    temp <<= 8u;
    temp += CCP4L;
    return temp;
}

/**
 * @brief CCP5 gets the capture value
 *
 * @return uint16_t capture value
 */
uint16_t Ccp5_GetCaptureValue(void)
{
    uint16_t temp = 0u;

    temp = PWM5L1;
    temp <<= 8u;
    temp += PWM5L0;
    return temp;
}

/**
 * @brief CCP3 set capture mode
 *
 * @param Mode Capture mode
 */
void Ccp3_SetCaptureMode(Ccp_CaptureModeType Mode)
{
    CCP3CTL &= 0xf0u;
    CCP3CTL |= (uint8_t)Mode;
}

/**
 * @brief CCP4 set capture mode
 *
 * @param Mode Capture mode
 */
void Ccp4_SetCaptureMode(Ccp_CaptureModeType Mode)
{
    CCP4CTL &= 0xf0u;
    CCP4CTL |= (uint8_t)Mode;
}
/**
 * @brief CCP5 set capture mode
 *
 * @param Mode Capture mode
 */
void Ccp5_SetCaptureMode(Ccp_CaptureModeType Mode)
{
    PWM5CTL0 &= 0xf0u;
    PWM5CTL0 |= (uint8_t)Mode;
}

/**
 * @brief CCP3 disable
 *
 */
void Ccp3_Disable(void)
{
    CCP3CTL &= 0xf0u;
}

/**
 * @brief CCP4 disable
 *
 */
void Ccp4_Disable(void)
{
    CCP4CTL &= 0xf0u;
}
/**
 * @brief CCP5 disable
 *
 */
void Ccp5_Disable(void)
{
    PWM5CTL0 &= 0xf0u;
}

/**
 * @brief CCP3 Deinitialization
 *
 */
void Ccp3_DeInit(void)
{
    T3CTL   = 0x00u;
    CCP3CTL = 0u;
    T3REH   = 0x00u;
    T3REL   = 0x00u;
    T3H     = 0u;
    T3L     = 0u;
    CCP3H   = 0u;
    CCP3L   = 0u;
}

/**
 * @brief CCP3 Deinitialization
 *
 */
void Ccp4_DeInit(void)
{
    T4CTL   = 0x00u;
    CCP4CTL = 0x00u;
    T4REH   = 0x00u;
    T4REL   = 0x00u;
    T4H     = 0u;
    T4L     = 0u;
    CCP4H   = 0u;
    CCP4L   = 0u;
}
/**
 * @brief CCP3 Deinitialization
 *
 */
void Ccp5_DeInit(void)
{
    T1CTL    = 0x00u;
    PWM5CTL0 = 0u;
    T1H      = 0u;
    T1L      = 0u;
    PP2      = 0u;
    PP1      = 0u;
    PWM5L1   = 0u;
    PWM5L0   = 0u;
    T1CLKSE  = 1u;
}

/* EOF */
